package com.zyh.map;

import com.zyh.map.domain.Teacher;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @Author zyh
 * @Date 2024/8/2 13:59
 * @Description 外部比较器
 */

public class TreeMapDemo3 {
    public static void main(String[] args) {
        TreeMap<Teacher, String> map = new TreeMap<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                return o1.getAge()>o2.getAge()?1:(o1.getAge()<o2.getAge()?-1:0);
            }
        });
        map.put(new Teacher("zyh",20),"yzh");
        map.put(new Teacher("cxk",22),"yzh");
        map.put(new Teacher("dz",10),"yzh");
        map.put(new Teacher("wy",90),"yzh");
        Set<Map.Entry<Teacher, String>> entries = map.entrySet();
        for (Map.Entry<Teacher, String> entry : entries) {
            System.out.println(entry.getKey()+"--"+entry.getValue());
        }
    }
}
